import Vue from 'vue'
import VueRouter from 'vue-router'
import CustomerManagement from '../components/customer/CustomerManagement.vue'
import AddCustomer from '../components/customer/AddCustomer.vue'
import OrderManagement from '@/components/order/OrderManagement.vue'
import OrderApplication from '@/components/order/OrderApplication.vue'
import OrderList from '@/components/order/orderList.vue'
import ProjectManagement from '../components/ProjectManagement.vue'
import TraderOrder from '@/components/order/TraderOrder.vue'
import OrderApprove from '@/components/order/OrderApprove.vue'
import jingLiApplication from '@/components/order/jingLiApplication.vue'
import LoginAndReg from '@/components/order/LoginAndReg.vue'
import jingLiApplicationDetail from '@/components/order/jingLiApplicationDetail.vue'
import ZongJianApplication from '@/components/order/ZongJianApplication.vue'
import UpdateOrder from '@/components/order/UpdateOrder.vue'
import SaleApplication from '@/components/order/SaleApplication.vue'
import ZongJianApplicationDetail from '@/components/order/ZongJianApplicationDetail.vue'
import ShenPi from '@/components/order/ShenPi.vue'
import UpdateOrderApplicaiton from '@/components/order/UpdateOrderApplicaiton.vue'
// 导入子组件
const CustomerList = () => import('../components/customer/CustomerList.vue')
const ProjectApplication = () => import('../components/project/ProjectApplication.vue')
const ProjectApproval = () => import('../components/project/ProjectApproval.vue')
const ProjectProducts = () => import('../components/project/ProjectProducts.vue')
const MyProject = () => import('../components/project/MyProject.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'  // 添加默认重定向到登录页
  },
  {
    path: '/login',
    name: 'Login',
    component: LoginAndReg
  },
  {
    path: '/customer',
    component: CustomerManagement,
    redirect: '/customer/customer-list', // 添加重定向
    children: [
      {
        path: 'customer-list',
        component: CustomerList
      },
      {
        path: 'add-customer',
        component: AddCustomer
      }
    ]
  },
  {
    path: '/order',
    component: OrderManagement,
    children: [
      {
        path: 'order_application',
        component: OrderApplication
      },
      {
        path: 'orderList',
        component: OrderList
      },
      {
        path: 'traderOrder',
        name: 'TraderOrder',
        component: TraderOrder
      },
      {
        path: 'orderApprove',
        name: 'OrderApprove',
        component: OrderApprove
      },
      {
        path: 'jingLiApplication',
        component: jingLiApplication
      },
      {
        path: 'jingLiApplicationDetail',
        component: jingLiApplicationDetail
      },
      {
        path: 'zongJianApplication',
        component: ZongJianApplication
      },
      {
        path: 'updateOrder',
        component: UpdateOrder
      },
      {
        path: 'saleApplication',
        component: SaleApplication
      },
      {
        path: 'zongJianApplicationDetail',
        component: ZongJianApplicationDetail
      },
      {
        path: 'shenPi',
        component: ShenPi
      },
      {
        path: 'updateOrderApplicaiton',
        component: UpdateOrderApplicaiton
      }
    ]
  },
  {
    path: '/project',
   component: ProjectManagement,
   redirect: '/project/application',
   children: [
    {
       path: 'application',
       component: ProjectApplication
    },
    {
       path: 'approval',
       component: ProjectApproval
    },
    {
       path: 'products',
       component: ProjectProducts
    },
    {
      path: 'projects',
       component: MyProject
     }
   ]
   },
]

const router = new VueRouter({
  routes
})

// 添加全局错误处理
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') {
      throw err
    }
  })
}

export default router
